import 'package:eplorex/pages/my/user_profile_page.dart';
import 'package:flutter/material.dart';

import '../../dao/follow_dao.dart';
import '../../models/follow/follow_model.dart';

class FollowItemWidget extends StatefulWidget {
  const FollowItemWidget({super.key, required this.followModel});

  final FollowModel followModel;

  @override
  State<FollowItemWidget> createState() => _FollowItemWidgetState();
}

class _FollowItemWidgetState extends State<FollowItemWidget> {
  int _followStatus = 0;

  @override
  void initState() {
    _followStatus = widget.followModel.relation!.toInt();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Wrap(
            spacing: 10,
            crossAxisAlignment: WrapCrossAlignment.start,
            children: [
              GestureDetector(
                onTap: () => {
                  Navigator.of(context)
                      .push(MaterialPageRoute(
                          builder: (_) => new UserProfilePage(
                              userId: widget.followModel.userId!)))
                      .then((value) => {
                            // setState(() {
                            //   _followStatus = !_needFresh;
                            // })
                          })
                },
                child: CircleAvatar(
                  radius: 18,
                  //todo 增加默认头像兜底
                  backgroundImage: NetworkImage(widget.followModel.avatar!),
                ),
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    widget.followModel.userName!,
                    style: TextStyle(fontSize: 16),
                  ),
                  Text("关注你了"),
                ],
              )
            ],
          ),
          if (_followStatus == 2) //被关注
            FilledButton(
              onPressed: () {
                FollowDao.followUser(widget.followModel.userId!, true);
                setState(() {
                  _followStatus = 5;
                });
              },
              child: Text("回关"),
              style: ButtonStyle(
                  minimumSize: MaterialStateProperty.all(Size(60, 30)),
                  padding: MaterialStateProperty.all(EdgeInsets.zero)),
            )
          else
            OutlinedButton(
              onPressed: () => {},
              child: Text("戳一下"),
              style: ButtonStyle(
                  minimumSize: MaterialStateProperty.all(Size(60, 30)),
                  padding: MaterialStateProperty.all(EdgeInsets.zero)),
            )
        ],
      ),
    );
  }
}
